<?php
/**
 * Created by JetBrains PhpStorm.
 * User: user
 * Date: 3/17/12
 * Time: 9:41 PM
 * To change this template use File | Settings | File Templates.
 */
class Admin_ArticleCategoryController extends Zend_Controller_Action{

    public function init(){
		parent::init();
        $layoutPath = APPLICATION_PATH  . '/templates/admin/default';
        $option = array ('layout' => 'index',
                   'layoutPath' => $layoutPath );
        Zend_Layout::startMvc ($option );

	}
    public function indexAction(){
        $article_category = new Admin_Model_ArticleCategory();
        $this->view->article_categorys = $article_category->fetchAll();
    }
    public function addAction(){
        $form = new Admin_Form_ArticleCategory();
        $form->submit->setLabel('Thêm');
        $this->view->form = $form;
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $ten = $form->getValue('ten');
                $mo_ta = $form->getValue('mo_ta');
                $ma_loai_cha = (int)$form->getValue('ma_loai_cha');
                $thu_tu_hien_thi = $form->getValue('thu_tu_hien_thi');
                $articlecategory = new Admin_Model_ArticleCategory();
                $articlecategory->addArticleCategory($ten, $mo_ta, $ma_loai_cha, $thu_tu_hien_thi);
                $this->_helper->redirector('index');
            } else {
                $form->populate($formData);
            }
        }
    }
    function editAction()
    {
        $form = new Admin_Form_ArticleCategory();
        $form->submit->setLabel('Lưu');
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $ma = (int)$form->getValue('ma');
                $ma_loai_cha = $form->getValue('ma_loai_cha');
                $ten = $form->getValue('ten');
                $mo_ta = $form->getValue('mo_ta');
                $thu_tu_hien_thi = $form->getValue('thu_tu_hien_thi');
                $articlecategory = new Admin_Model_ArticleCategory();
                $articlecategory->updateArticleCategory($ma, $ten, $mo_ta, $ma_loai_cha, $thu_tu_hien_thi);
                $this->_helper->redirector('index');
            } else {
                $form->populate($formData);
            }
        } else {

            $ma = $this->_getParam('ma', 0);
            if ($ma > 0) {
                $articlecategory = new Admin_Model_ArticleCategory();
                $form->populate($articlecategory->getArticleCategory($ma));
            }
        }
    }

    public function deleteAction(){
        if ($this->getRequest()->isPost()) {
            $del = $this->getRequest()->getPost('del');
            if ($del == 'Yes') {
                $ma = $this->getRequest()->getPost('ma');
                $articlecategory = new Admin_Model_ArticleCategory();
                $articlecategory->deleteArticleCategory($ma);
            }
            $this->_helper->redirector('index');
        } else {
            $ma = $this->_getParam('ma', 0);
            $articlecategorys = new Admin_Model_ArticleCategory();
            $this->view->articlecategory = $articlecategorys->getArticleCategory($ma);
        }
    }
}
 
